<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style>
    .cvs{
      border: solid 1px black;
    }
  </style>
</head>
<body>
  <canvas class="cvs"></canvas>
</body>
<script>

  const cvs = document.querySelector(".cvs")

  cvs.width = 400;
  cvs.height = 400;

  const ctx = cvs.getContext("2d");
  
  const img = new Image();
  img.src = "../imgs/2.png";
  img.onload = function(){
    ctx.clearRect(0,0,cvs.width,cvs.height);
    ctx.drawImage(this, 0, 192 / 4 * 2, 128 / 4, 192 / 4, 0, 100, 128 / 4, 192 / 4);
  }

  document.onclick = function(){
    let i = 0;
    let speedX = 5;
    let x = 0;
    setInterval(()=>{
      x += speedX;
      if(i === 3){
        i=0;
      }else{
        i++;
      }
      ctx.clearRect(0,0,cvs.width,cvs.height);
      ctx.drawImage(img, 128 / 4 * i, 192 / 4 * 2, 128 / 4, 192 / 4, x, 100, 128 / 4, 192 / 4);
    }, 150)
  }

  // 作业：运动的小人（可通过键盘方向键，控制方向）
  
</script>
</html>